//
//  DetailViewController.swift
//  MasterDetail2
//
//  Created by Max Chiodo on 9/30/14.
//  Copyright (c) 2014 SSW. All rights reserved.
//

import UIKit

class DetailViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


    // UI objects 
    
    @IBOutlet weak var details1: UILabel!
    @IBOutlet weak var choice1: UILabel!
    @IBOutlet weak var picker1: UIPickerView!

    // Functions
    
    var detailItem: AnyObject? {
        didSet {
            // Update the view.
            self.configureView()
        }
    }

    func configureView() {
        // Update the user interface for the detail item.
        if let detail: Record = self.detailItem as? Record {
            if let label = self.details1 {
                label.text = "XX" + detail.detailedDescription() + "XX"
            }
        }
        if let pick = self.picker1 {
            _pickerData = ["Susan", "Ale", "Lorenzo", "Luciano", "Max"]
            picker1.dataSource = self
            picker1.delegate = self
            if let lab2 = self.choice1 {
                lab2.text = (self.detailItem as Record).choice
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        self.configureView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // Picker view stuff
    
    var _pickerData = [String]()
    
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return _pickerData.count
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String
    {
        return _pickerData[row]
    }
    
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if let lab2 = self.choice1 {
            var t = _pickerData[row]
            lab2.text = t
            (self.detailItem as? Record)?.choice = t
            //self.parentViewController.
        }
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "showDetail" {
            //if let indexPath = self.sender. tableView.indexPathForSelectedRow() {
//                let object = objects[indexPath.row] as Record
               // (segue.destinationViewController as DetailViewController).detailItem = object
          //  (segue.sourceViewController as MasterViewController).  reloadRowsAtIndexPaths([0], withRowAnimation: .Automatic)

            //}
        }
    }

}

